using Chaotic.DataBase.DataAccess;
using Chaotic.DataBase.DBAccessLibrary.DBAccessLibrary;
using Chaotic.DataBase.DBAccessLibrary.Interface;
using Chaotic.DataBase.DBAccessLibrary.Model;

namespace Chaotic.DataBase.DBAccessLibrary.Grammar;

internal class OleGrammar : DBGrammar, IGrammar, ILocalGrammar
{
	public string GetChTableSql => string.Empty;

	public string? IsExistChTableSql => string.Empty;

	public OleGrammar(ConnectionModel? connect)
		: base(connect)
	{
	}

	private string? GetTypeFilter(FieldEntity? field, string sql)
	{
		string? typeName = field.TypeName;
		int.TryParse(field.Length, out var _);
		string? text = field.Length;
		string text2 = field.TypeName.ToUpper();
		if (text2 == "TIMESTAMP" || text2 == "DATE")
		{
			text = "";
		}
		if (!field.IsNull)
		{
			sql += " Not Null";
		}
		return string.Format(sql, field.TableName, field.Name, typeName, text);
	}

	public string? AddColumn(FieldEntity? field)
	{
		string text = "ALTER TABLE [{0}] ADD COLUMN {1} {2}{3}";
		if (!string.IsNullOrEmpty(field.DefaultValue))
		{
			text = text + " DEFAULT " + field.DefaultValue;
		}
		return GetTypeFilter(field, text);
	}

	public IEnumerable<string?> EditColumn(FieldEntity? field)
	{
		List<string?> list = new List<string?>();
		list.Add(GetTypeFilter(field, "ALTER TABLE {0} ALTER COLUMN {1} SET DATA TYPE {2}{3}"));
		string.IsNullOrEmpty(field.DefaultValue);
		return list;
	}

	public IEnumerable<string?> RemoveColumn(FieldEntity field)
	{
		List<string?> list = new List<string?>();
		list.Add($"ALTER TABLE {field.TableName} DROP COLUMN {field.TransName}");
		return list;
	}

	public IEnumerable<string?> EditPrimary(TableEntity table)
	{
		List<string?> list = new List<string?>();
		string text = table.PrimaryKeyName;
		string[] array = (from o in table.FileNodes
			where o.IsPrimaryKey
			select o.Name).ToArray();
		if (array.Length > 0)
		{
			if (!string.IsNullOrEmpty(text))
			{
				string? item = $"alter table {table.Name} drop constraint {text} ";
				list.Add(item);
			}
			else
			{
				text = "PK" + array[0];
			}
			list.Add(string.Format("ALTER TABLE {0} ADD CONSTRAINT {1} PRIMARY KEY({2})", table.Name, text, string.Join(",", array)));
		}
		return list;
	}

	public string? SelectTop(TableEntity table, int count)
	{
		string format = "SELECT TOP {1} * FROM [{0}]";
		return string.Format(format, table.TransName, count);
	}

	public string AppendTabText(string text)
	{
		return TabText(text);
	}

	public string GetErrorInfo(string errorMsg)
	{
		return string.Empty;
	}

	public string? GetProcParamInfo(ProcEntity entity)
	{
		if (entity == null)
		{
			return string.Empty;
		}
		string arg;
		if (entity.ChildNodes.Count > 0)
		{
			List<string> list = new List<string>();
			foreach (ProcParamEntity? childNode in entity.ChildNodes)
			{
				if (childNode.Status == "IN")
				{
					list.Add($"{Environment.NewLine}[{childNode.Name}({childNode.TypeName})]");
				}
				else
				{
					list.Add($"{Environment.NewLine}[{childNode.Name}({childNode.TypeName})-OUT]");
				}
			}
			arg = "(" + string.Join(",", list.ToArray()) + ")";
		}
		else
		{
			arg = "()";
		}
		return string.Format("{0} {1}{2}", "EXEC", entity.Name, arg);
	}

	public string GetPageingSql(string sql, decimal index, decimal count)
	{
		return sql;
	}

	public DatabaseType GetConnectionStr(out string connectStr)
	{
		connectStr = _connect.ConnectionStr;
		return DatabaseType.OleDBSupported;
	}
}
